<?php
define("ENTER",chr(13).chr(10));
class HTMLUtils
{
	private $arrPageFlag 	= array();
	private $arrSearch 		= array();
	private $arrReplace		= array();
	
	function __construct()
	{
		$this->arrSearch 	= array ("'<script[^>]*?>.*?</script>'si", "'<[\/\!]*?[^<>]*?>'si",  "'([\r\n])[\s]+'", "'&(quot|#34);'i",  "'&(amp|#38);'i", "'&(lt|#60);'i",  "'&(gt|#62);'i", "'&(nbsp|#160);'i",   "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(\d+);'e");
		$this->arrReplace 	= array ("",  "",   "\\1",  "\"", "&", "<", ">",   " ", chr(161), chr(162), chr(163), chr(169), "chr(\\1)"); 
	}
	
	public function main()
	{

		
		$strUrl 		= "http://ent.sina.com.cn/film/chinese/more.html";
		$strStartFlag 	= '<li>';
		$strEndFlag 	= '<font size=1 color=#6666cc>(';
		$page 		= 1;
		$isList 	= 1;
		$startPos	= 0;
		$endPos		= 0;
		$subffix	= '';
		
		$strNextStartFlag = '<!--正文开始-->';
		$strEndStartFlag = '<!--正文内容结束-->';
		
		$strTitleStartFlag = '<h1>';
		$strTtitleStartFlag = '</h1>';
		
		$strImageStartFlag = '<h1>';
		$strImageStartFlag = '</h1>';
		
		$objHtmlUtils = new HtmlUtils();
		$objHtmlUtils->vdSetFlag(1, 2, $strStartFlag, $strEndFlag);
		$objHtmlUtils->vdSetFlag(2, 0, $strNextStartFlag, $strEndStartFlag);
		$i = 0;
		$arrDataList = $objHtmlUtils->arrGetLabelContent($objHtmlUtils->strGetContent($strUrl, 1), "a", "href");
		foreach ($arrDataList as $key => $value){
			echo '================'.$key.'<br>';
			if($i < 6){
				echo '---------'.$key.'<br>';
				echo $objHtmlUtils->strFilterLabel($objHtmlUtils->strGetContent($key, 2, true), 'h1').'<br>';
			}
			$i++;
		}
	}
	
	public function getBusPathFromDDmap($content)
	{
		$arrReturn = array();
		
		$iStartPos = 0;
		$strContent = strtolower($content);
		for($i = 1; $i < 100; $i++){
			$strDataLabel = "<n>desc$i</n>";
			$iStartPos 	= strpos($strContent, $strDataLabel, $iStartPos);
			if($iStartPos !== false){
				$iDataStartPos 	= strpos($strContent, "<string>", $iStartPos);
				if($iDataStartPos !== false){
					$iDataEndPos 	= strpos($strContent, "</string>", $iStartPos);
					if($iDataEndPos !== false){
						$arrReturn[] = substr($content, $iDataStartPos + 8, $iDataEndPos - $iDataStartPos - 8);
						$iStartPos = $iDataEndPos;
					}else
						break;
				}else
					break;
			}else
				break;
		}
		return  $arrReturn;	
	}
	
	
	public function arrGetLabelContent($strContent, $strLabel)
	{
		$strLabelPre = '>';
		$strLabelEnd = '</' . $strLabel . '>';
		$strLabelStart = '<' . $strLabel;
		
		$strContent = strtolower($strContent);
		
		$arrReturn = array();
		$iStartPos 	= stripos($strContent, $strLabelStart);
		
		while($iStartPos){
			$iEndPos = stripos($strContent, $strLabelEnd, $iStartPos);
			if($iEndPos){
				$iPrePos = stripos($strContent, $strLabelPre, $iStartPos);
				if($iPrePos){
					if($strLabel == 'a'){
						$iHerfStartPos = stripos($strContent, "href=", $iStartPos);
						if($iHerfStartPos and $iHerfStartPos < $iPrePos){
							if(substr($strContent, $iHerfStartPos + 5, 1) == "\""){
								$iHerfEndPos = stripos($strContent, "\"", $iHerfStartPos);
								if($iHerfEndPos){
									$arrReturn[substr($strContent, $iHerfStartPos + 5, $iHerfEndPos - $iHerfStartPos + 5)] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
								}
							}else{
								$iHerfEndPos = stripos($strContent, " ", $iHerfStartPos);
								if($iHerfEndPos and $iHerfEndPos < $iPrePos){
									echo '----------'.ENTER;
									$arrReturn[substr($strContent, $iHerfStartPos + 5, $iHerfEndPos - $iHerfStartPos - 5)] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
								}else{
									echo '++++++++++++'.ENTER;
									$arrReturn[substr($strContent, $iHerfStartPos + 5, $iPrePos - $iHerfStartPos - 5)] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
								}
							}
						}
					}else
						$arrReturn[] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
					
					$iStartPos 	= stripos($strContent, $strLabelStart, $iEndPos);
				}
			}
		}
		return $arrReturn;
	}
	
	function getbusnamefromStr($strLine)
	{
		$arrReturn = array();
		$iStartPos 	= stripos($strLine, "��");
		while($iStartPos !== false){
			$iEndPos = strpos($strLine, "(", $iStartPos);
			if($iEndPos){
				$arrReturn[] = substr($strLine, $iStartPos + 2, $iEndPos - $iStartPos - 4);
			}else
				break;
			$iStartPos = strpos($strLine, "��", $iEndPos);;
		}
		return $arrReturn;
	}
	
	
	public function vdSetFlag($iPage, $iNextPage, $strStartFlag, $strEndFlag)
	{
		$arrFlag = array();
		$arrFlag['start'] 			= $strStartFlag;
		$arrFlag['end']				= $strEndFlag;
		$arrFlag['nextPage'] 		= $iNextPage;
		$this->arrPageFlag[$iPage] 	= $arrFlag;
	}
	
	public function strGetContent($strUrl, $iPage, $sourceCharset = "GB2312", $targetCharget = "UTF-8")
	{
		if(!is_array($this->arrPageFlag[$iPage]))
			return '';

		$arrFlag = $this->arrPageFlag[$iPage];
		
		$strHtmlCode = @file_get_contents($strUrl);
		$strHtmlCode = mb_convert_encoding($strHtmlCode, $targetCharget, $sourceCharset);
		if(empty($arrFlag['start']) && empty($arrFlag['end']))
			return $strHtmlCode;
		else if ($strHtmlCode){
			if(empty($arrFlag['start']))
				$iStartPos 	= 0;
			else
				$iStartPos 	= mb_stripos($strHtmlCode, $arrFlag['start'], 0, $targetCharget);
				
			if(empty($arrFlag['end']))
				$iEndPos = mb_strlen($strHtmlCode, $targetCharget);
			else
				$iEndPos = mb_strrpos($strHtmlCode, $arrFlag['end'], 0, $targetCharget);
			
			return mb_substr($strHtmlCode, $iStartPos + mb_strlen($arrFlag['start'], $targetCharget), $iEndPos - $iStartPos - mb_strlen($arrFlag['start'], $targetCharget), $targetCharget);
		}
		
		return $strHtmlCode;
	}
	
	
	public function strGetHTMLSource($strUrl)
	{
		return @file_get_contents($strUrl);
	}
	
	public function strConvertCharset($strHTMLSource, $sourceCharset = "GB2312", $targetCharget = "UTF-8")
	{
		return mb_convert_encoding($strHTMLSource, $targetCharget, $sourceCharset);
	}
	
	public function strGetLabelContent($strHTMLSource, $startTag, $endTag, $sourceCharset = "GB2312", $targetCharget = "UTF-8")
	{
		if(empty($startTag) && empty($endTag))
			return $strHTMLSource;
		else if ($strHTMLSource){
			if(empty($startTag))
				$iStartPos 	= 0;
			else
				$iStartPos 	= mb_stripos($strHTMLSource, $startTag, 0, $targetCharget);
				
			if(empty($endTag))
				$iEndPos = mb_strlen($strHTMLSource, $targetCharget);
			else
				$iEndPos = mb_strrpos($strHTMLSource, $endTag, 0, $targetCharget);
			
			return mb_substr($strHTMLSource, $iStartPos + mb_strlen($startTag, $targetCharget), $iEndPos - $iStartPos - mb_strlen($startTag, $targetCharget), $targetCharget);
		}
	}
	
	public function arrGetLabelAllContent($strContent, $strLabel, $strKeyAttribute = '')
	{
		$strLabelPre = '>';
		$strLabelEnd = '</' . $strLabel . '>';
		$strLabelStart = '<' . $strLabel;
		
		$arrReturn = array();
		$iStartPos 	= stripos($strContent, $strLabelStart);
		
		while($iStartPos){
			$iEndPos = stripos($strContent, $strLabelEnd, $iStartPos);
			if($iEndPos){
				$iPrePos = stripos($strContent, $strLabelPre, $iStartPos);
				if($iPrePos){
					$iAttriStartPos = stripos($strContent, $strKeyAttribute, $iStartPos);
					if($iAttriStartPos and $iAttriStartPos < $iPrePos){
						if(substr($strContent, $iAttriStartPos + strlen($strKeyAttribute) + 1, 1) == "\""){
							$iAttriEndPos = stripos($strContent, "\"", $iAttriStartPos);
							if($iAttriEndPos){
								$arrReturn[substr($strContent, $iAttriStartPos + strlen($strKeyAttribute) + 1, $iAttriEndPos - $iAttriStartPos + strlen($strKeyAttribute) + 1)] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
							}
						}else{
							$iAttriEndPos = stripos($strContent, " ", $iAttriStartPos);
							if($iAttriEndPos and $iAttriEndPos < $iPrePos){
								$arrReturn[substr($strContent, $iAttriStartPos + strlen($strKeyAttribute) + 1, $iAttriEndPos - $iAttriStartPos - strlen($strKeyAttribute) - 1)] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
							}else{
								$arrReturn[substr($strContent, $iAttriStartPos + strlen($strKeyAttribute) + 1, $iPrePos - $iAttriStartPos - strlen($strKeyAttribute) - 1)] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
							}
						}
					}else
						$arrReturn[] = substr($strContent, $iPrePos + strlen($strLabelPre), $iEndPos - $iPrePos);
				
					$iStartPos 	= stripos($strContent, $strLabelStart, $iEndPos);
				}
			}
		}
		return $arrReturn;
	}
	
	public function strFilterLabel($strContent, $strLabel)
	{
		$strLabelEnd 	= '</' . $strLabel . '>';
		$strLabelStart 	= '<' . $strLabel . '>';
		
		$iStartPos 	= stripos($strContent, $strLabelStart);
		while($iStartPos){
			$iEndPos = stripos($strContent, $strLabelEnd, $iStartPos);
			if($iEndPos){
				$strContent = substr_replace($strContent, '', $iStartPos, $iEndPos - $iStartPos + strlen($strLabelEnd));
				$iStartPos 	= stripos($strContent, $strLabelStart);
			}
		}
		return $strContent;
	}
	
	public function strGetLabelText($strContent, $strLabel, $targetCharget = "UTF-8")
	{
		$strLabelEnd 	= '</' . $strLabel . '>';
		$strLabelStart 	= '<' . $strLabel . '>';
		
		$iStartPos 	= mb_stripos($strContent, $strLabelStart, 0, $targetCharget);
		echo 'iStartPos='.$iStartPos.'<br>';
		if($iStartPos >= 0){
			$j = 0;// inner the same tags count with start tag.
			
			$iPosCache = $iStartPos + mb_strlen($strLabelStart, $targetCharget);
			$iPos = mb_stripos($strContent, $strLabelStart, $iPosCache, $targetCharget);
			while($iPos){
				$j++;
				
				$iPosCache = $iPos + mb_strlen($strLabelStart, $targetCharget);
				$iPos = mb_stripos($strContent, $strLabelStart, $iPosCache, $targetCharget);
			}
			echo 'j='.$j.'<br>';
			echo 'iPosCache='.$iPosCache.'<br>';
			
			for($i = 0; $i < $j; $i++){
				$iPos = mb_stripos($strContent, $strLabelEnd, $iPosCache, $targetCharget);
				if($iPos == -1){//on has end tag in inner tag 
					return "";
				}else
					$iPosCache = $iPos + mb_strlen($strLabelEnd, $targetCharget);
			}
			
			echo 'iPosCache2='.$iPosCache.'<br>';
			
			$iEndPos = mb_stripos($strContent, $strLabelEnd, $iPosCache, $targetCharget);
			echo '$iEndPos='.$iEndPos.'<br>';
			if($iEndPos){
				return mb_substr($strContent, $iStartPos + mb_strlen($strLabelStart, $targetCharget), $iEndPos - $iStartPos  - mb_strlen($strLabelStart, $targetCharget), $targetCharget);
				//$iStartPos 	= stripos($strContent, $strLabelStart);
			}else
				return "";
		}else
			return "";
	}
	
	public function getTagContent($strHTMLSource, $tag, $arrFilterTag = array())
    {
    	$returContent = '';
    	
 		if(eregi("<$tag>(.*)</$tag>", $strHTMLSource ,$out)){ 
		     $returContent = $this->strip_selected_tags_Content($out[0],$arrFilterTag); 
		}
		
		return $returContent;
    }
    
	public function strip_selected_tags($text, $tags = array())
    {
        $args = func_get_args();
        $text = array_shift($args);
        $tags = func_num_args() > 2 ? array_diff($args,array($text)) : (array)$tags;
        foreach ($tags as $tag){
            if( preg_match_all( '/<'.$tag.'[^>]*>([^<]*)<\/'.$tag.'>/iu', $text, $found) ){
                $text = str_replace($found[0],$found[1],$text);
            }
        }

        return preg_replace( '/(<('.join('|',$tags).')(\n|\r|.)*\/>)/iu', '', $text);
    }
    
    public function strip_selected_tags_Content($text, $tags = array())
    {
        $args = func_get_args();
        $text = array_shift($args);
        $tags = func_num_args() > 2 ? array_diff($args,array($text)) : (array)$tags;

        foreach ($tags as $tag){
           $text = preg_replace('/<'.$tag.'[^>]*>([^<]*)<\/'.$tag.'>/iu', '', $text);
        }

        return $text;
    }
    
}
